package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import com.sample.action.form.ZcPolicyForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * 慢性病报销政策
 * @author 深深
 * @version1.0
 *
 */
public class ZcPolicy {

	//定义属性
	private int zc_id;//政策编号
	private String runyear;//执行年度
	private String rate;//比例
	private String maxLine;//封顶线
	private String runRate;//查询比例
	
	private JdbcUtils jdbcUtils ;
	
	//构造函数
	public ZcPolicy(){
		jdbcUtils=new JdbcUtils();
	}

	public ZcPolicy(String runyear, String rate, String maxLine, String runRate, int zc_id) {
		super();
		this.runyear = runyear;
		this.rate = rate;
		this.maxLine = maxLine;
		this.runRate = runRate;
		this.zc_id= zc_id;
	}
	/**
	 * 下拉列表
	 */
	
	public List<ZcPolicy> findAll()throws Exception{
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<ZcPolicy> zcpolicyList =new ArrayList<ZcPolicy>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造SQL语句
			String sql="select * from zc_policy where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				 String runyear=rs.getString("runyear");
				 String rate=rs.getString("rate");
				 String maxLine= rs.getString("maxLine");
				 String runRate =rs.getString("runRate");
				 int id= rs.getInt("zc_id");
				  ZcPolicy zcPolicy= new ZcPolicy(runyear, rate, maxLine, runRate, id);
				
				//调用顺序列表的add方法，加入元素
				zcpolicyList.add(zcPolicy);
				
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			throw new Exception("获取信息失败，Exception_bean:" + e.getCause().getClass() + "," + e.getCause().getMessage());
			
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return zcpolicyList;
	}
	/**
	 * 添加政策
	 * @return
	 */
	public void addZcPolicy(ZcPolicyForm form)throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "insert into zc_policy(runyear,rate,maxLine,runRate,zc_id) values('"
					+ form.getRunyear() + "','" + form.getRate()+ "','" + form.getMaxLine() + "','"+form.getRunRate()+"','"+form.getZc_id()+"')";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			 e.printStackTrace();
			 throw new Exception("添加政策失败，Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}		
	}
	
	/**
	 * 删除政策
	 * @return
	 */
	public void delZcpolicy()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "delete from zc_policy where zc_id='"+this.zc_id+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			 e.printStackTrace();
			 throw new Exception("删除政策失败，Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}
	}
	/**
	 * 按照id加载政策信息 
	 * @return
	 */
	public void load()throws Exception{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null; 
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "select * from  zc_policy where zc_id='"+this.zc_id+"'";
			// 4:执行SQL并返回结果
			rs=stmt.executeQuery(sql);
			if(rs.next()){
				this.runRate =rs.getString("runRate");
				this.rate =rs.getString("rate");
				this.runyear =rs.getString("runyear");
				this.maxLine =rs.getString("maxLine");
				this.zc_id =rs.getInt("zc_id");
			}
			// 5:关闭资源
		} catch (SQLException e) {
			 e.printStackTrace();
			 throw new Exception("加载政策信息失败，Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}	
	}
	
	/**
	 * 编辑政策信息
	 * @return
	 */
	public void editZcPolicy(ZcPolicyForm form)throws Exception {

		Connection conn = null;
		Statement stmt = null;
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			// 3:构造插入SQL
			String sql = "update  zc_policy set runyear='"+form.getRunyear()+"',runRate='"+form.getRunRate()+"',rate='"+form.getRate()+"',maxLine='"+form.getMaxLine()+"' where zc_id='"+form.getZc_id()+"'";
			// 4:执行SQL并返回结果
			stmt.executeUpdate(sql);
			// 5:关闭资源
		} catch (SQLException e) {
			 e.printStackTrace();
			 throw new Exception("编辑政策失败Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}

	}
	
	/**
	 * 分页显示z政策信息
	 * @return
	 */
	public Pager<ZcPolicy> list(int page,int size,ZcPolicyForm ZcPolicyForm)throws Exception{

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs =null;
		Pager<ZcPolicy> zcPolicys=new Pager<ZcPolicy>();
		zcPolicys.setPage(page);
		zcPolicys.setSize(size);
		try {
			conn = jdbcUtils.getConn();
			stmt = conn.createStatement();
			int start=(page-1)*size;
			int limit=size;
			//查询SQL
			String selectSql="select * from zc_policy where 1=1";
			if(ZcPolicyForm!=null&&ZcPolicyForm.getRunyear()!=null&&!"".equals(ZcPolicyForm.getRunyear())){
				selectSql +=" and runyear like'%"+ZcPolicyForm.getRunyear()+"%'";
			}
			//统计SQL
			String countSql ="select count(*)as c from ("+selectSql+") as t1";
			//分页SQL
			 String pageSql=selectSql+" limit "+start+","+limit;
			 rs= stmt.executeQuery(countSql);
			 int total=0;
			 while(rs.next()){
				 total=rs.getInt("c");
			 }
			 zcPolicys.setTotal(total);
			 //查询分页信息
			 rs= stmt.executeQuery(pageSql);
			 while(rs.next()){
				 String runyear=rs.getString("runyear");
				 String rate=rs.getString("rate");
				 String maxLine= rs.getString("maxLine");
				 String runRate =rs.getString("runRate");
				 int id= rs.getInt("zc_id");
				  ZcPolicy zcPolicy= new ZcPolicy(runyear, rate, maxLine, runRate, id);
				  zcPolicys.getDataSets().add(zcPolicy);
			 }
			 zcPolicys.init();
			// 5:关闭资源
		} catch (SQLException e) {
			 e.printStackTrace();
			 throw new Exception("分页显示失败，Exception_bean:<--" + e+"->");
		} finally {
			jdbcUtils.close(conn, stmt);
		}
		return zcPolicys;

	}


	//get、set方法
	

	public String getRunyear() {
		return runyear;
	}

	public int getZc_id() {
		return zc_id;
	}

	public void setZc_id(int zc_id) {
		this.zc_id = zc_id;
	}

	public void setRunyear(String runyear) {
		this.runyear = runyear;
	}

	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}

	public String getMaxLine() {
		return maxLine;
	}

	public void setMaxLine(String maxLine) {
		this.maxLine = maxLine;
	}

	public String getRunRate() {
		return runRate;
	}

	public void setRunRate(String runRate) {
		this.runRate = runRate;
	}

	public JdbcUtils getJdbcUtils() {
		return jdbcUtils;
	}

	public void setJdbcUtils(JdbcUtils jdbcUtils) {
		this.jdbcUtils = jdbcUtils;
	}
	
 
}
